<?php
    class user_controller extends Controller
    {
        private $langMap;
        private $userModel;
        
        public function __construct(){
            parent::__construct();
            $this->load->library('auth');
            $this->load->helper('html_helper');
            $this->langMap = $this->load->lang('user/user');
            $this->userModel = $this->load->model('user');	
        }
        
        public function index(){
            $this->_renderRedirect();
        }

        /*
        	@param string? redirect encoded url
           	@return void
        */
        public function login($redirect = null){
        	if ($this->userModel->isLogin()) return $this->_renderRedirect($redirect);
        	$this->load->controller('_header')->display();
            $this->load->controller('_sidebar')->display();
            $this->load->view(transLang('user/login'))->display();
            $this->load->controller('_footer')->display();
        }
        
        /*
        	@param string? redirect encoded url
            @return void
        */
        public function regist($redirect = null){
        	if ($this->userModel->isLogin()) return $this->_renderRedirect($redirect);
        	$this->load->controller('_header')->display();
            $this->load->controller('_sidebar')->display();
            $this->load->view(transLang('user/regist'))->display();
            $this->load->controller('_footer')->display();
        }

        /*
            @return void
        */
        public function doLogin(){
        	if (!isset($_POST['username']) || !Validator::isName($_POST['username']))
            	return false;
            elseif (!isset($_POST['password']) || !Validator::isPassword($_POST['password']))
            	return false;
           	else{
            	$username = strval($_POST['username']);
                $password = strval($_POST['password']);
            	if (isset($_POST['rememberInCookie']) && 0 === strcasecmp('on', $_POST['rememberInCookie']))
                	$remember = true;
                else
                	$remember = false;
                    
                $this->userModel->login($username, $password, $remember);
                
                $this->_renderRedirect($redirect);
            }
        }

        /*
        	@return void
        */
        public function doRegist(){
			if (!isset($_POST['username']) || !Validator::isName($_POST['username']))
            	return false;
            elseif (!isset($_POST['password']) || !Validator::isPassword($_POST['password']))
            	return false;
            elseif (!isset($_POST['repeatPassword']) || !Validator::isPassword($_POST['repeatPassword']))
            	return false;
            elseif (0 !== strcmp($_POST['repeatPassword'], $_POST['password']))
            	return false;
            else{
            	$options = array();
            	$username = strval($_POST['username']);
                $password = strval($_POST['password']);
                
            	if (isset($_POST['email']) && Validator::isEmail($_POST['email'])){
                	$options['email'] = strval($_POST['email']);
                }
                
                if (isset($_POST['mobile']) && Validator::isMobileNum($_POST['mobile'])){
                	$options['mobile'] = strval($_POST['mobile']);
                }
                
                $this->userModel->regist($username, $password, $options);
                $this->_renderRedirect($redirect);
            }
        }
        
        /*
    		@param string redirect encoded url
			@return void
        */
        public function doLogout($redirect = null){
			$this->userModel->logoff();
            $this->_renderRedirect($redirect);
        }
        
        /*
        	@param string? need to redirect base64 encoded url
            @return void
        */
        private function _renderRedirect($redirect = null){
        	if (empty($redirect)) 
            	$redirect = SYS_LOGIN_HOME;
            else 
            	$redirect = base64_decode($redirect);
            
            header('Location: ' . $redirect);
            die;
        }
    }